﻿using BF.Model;
using BF.Templates;
using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BF.Web.pages.CodeBuilder
{
    public partial class Builder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (txtClassName.Value.Trim() != string.Empty)
            {
                string dirpath = Server.MapPath("tempfile");

                if (!Directory.Exists(dirpath))
                {
                    Directory.CreateDirectory(dirpath);
                }
                try
                {
                    List<string> list = txtClassName.Value.Trim().Split(',').ToList<string>();
                    if (list.Count > 0)
                    {
                        #region 通过反射取得要生成的类
                        Assembly ass = Assembly.Load("BF.Templates"); //获取程序集
                        Type myclass = null;
                        MethodInfo mymethod = null;
                        foreach (var name in list)
                        {
                            foreach (ListItem item in chkboxType.Items)
                            {
                                if (item.Selected)
                                {
                                    Type[] mytypes = ass.GetTypes();
                                    foreach (Type t in mytypes)
                                    {
                                        if (t.Name == item.Text)
                                        {
                                            myclass = t;
                                            break;
                                        }
                                    }
                                    Object obj = Activator.CreateInstance(myclass, name);
                                    mymethod = myclass.GetMethod("TransformText"); //取TransformText()函数
                                    String pageContent = mymethod.Invoke(obj, null).ToString();
                                    //创建文件夹
                                    string dirpath_Library = null;
                                    dirpath_Library = dirpath + @"\" + item.Text + @"\" + string.Format(item.Value, name) + "Library";
                                    if (!Directory.Exists(dirpath_Library))
                                    {
                                        Directory.CreateDirectory(dirpath_Library);
                                    }

                                    //创建文件
                                    File.AppendAllText(dirpath_Library + @"\" + string.Format(item.Value, name) + ".cs", pageContent, Encoding.UTF8);
                                }
                            }

                            string regsterContent = new Regster(name).TransformText();
                            //创建或追加注册代码
                            File.AppendAllText(dirpath + @"\Regster.txt", regsterContent, Encoding.UTF8);

                            string usingCodeContent = new UsingCode(name).TransformText();
                            //创建或追加注册代码uging
                            File.AppendAllText(dirpath + @"\usingCode.txt", usingCodeContent, Encoding.UTF8);

                            string editContent = new PageEdit(name).TransformText();
                            //创建页面目录
                            string pageEditPath = dirpath + @"\Page\" + name + "Page";
                            if (!Directory.Exists(pageEditPath))
                            {
                                Directory.CreateDirectory(pageEditPath);
                            }
                            //创建文件
                            File.AppendAllText(pageEditPath + @"\Edit.aspx", editContent,Encoding.UTF8);

                            string editCodeBehindContent = new PageEditCodeBehind(name).TransformText();
                            File.AppendAllText(pageEditPath + @"\Edit.aspx.cs", editCodeBehindContent, Encoding.UTF8);

                            string editDesignerContent = new PageEditDesigner(name).TransformText();
                            File.AppendAllText(pageEditPath + @"\Edit.aspx.designer.cs", editDesignerContent, Encoding.UTF8);

                            //创建文件
                            string listContent = new PageList(name).TransformText();
                            File.AppendAllText(pageEditPath + @"\List.aspx", listContent, Encoding.UTF8);

                            string listCodeBehindContent = new PageListCodeBehind(name).TransformText();
                            File.AppendAllText(pageEditPath + @"\List.aspx.cs", listCodeBehindContent, Encoding.UTF8);

                            string listDesignerContent = new PageListDesigner(name).TransformText();
                            File.AppendAllText(pageEditPath + @"\List.aspx.designer.cs", listDesignerContent, Encoding.UTF8);
                        }
                        #endregion
                    }
                    //压缩
                    SharpZip.PackFiles(Server.MapPath("code.rar"), dirpath);
                    //下载
                    FileDown.DownLoadold("code.rar");

                }
                finally
                {
                    Directory.Delete(dirpath, true);
                    File.Delete(Server.MapPath("code.rar"));
                }
            }
        }
    }
}